package com.estate.parking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.Parking;
import com.estate.mapper.ParkingMapper;
import com.estate.parking.feign.QuartersFeign;
import com.estate.parking.service.ParkingService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper, Parking> implements ParkingService {

    @Resource
    private QuartersFeign quartersFeign;
    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<Parking> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<Parking> list = list(wrapper);
        list.forEach(e->{
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
        });
        PageInfo<Parking> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult savePacking(Parking parking) {
        parking.setStatus(0);
        parking.setIsDelete(0);
        log.info("新增parking数据:{}", parking);
        boolean b = save(parking);
        if (!b) {
            return ResultTool.fail(501, "新增车位失败");
        }
        return ResultTool.success("新增成功");
    }

    @Override
    public JsonResult updateParking(Parking parking) {
        log.info("修改parking数据:{}", parking);
        boolean b = updateById(parking);
        if (!b) {
            return ResultTool.fail(503, "修改车位失败");
        }
        return ResultTool.success("修改成功");
    }

    @Override
    public JsonResult deleteParking(String parkingId) {
        log.info("删除parking数据:{}", parkingId);
        boolean b = removeById(parkingId);
        if (!b) {
            return ResultTool.fail(502, "删除车位失败");
        }
        return ResultTool.success("删除成功");
    }


}
